/**
 * Created by zhanghaibin on 2016/12/28.
 */
require(['js/domReady','js/service/area','js/service/user','js/service/search'],function(domReady,area,user,search){
    domReady(function(){
        //省下拉
        var oSpan = document.getElementById('spnArea');
        var oUl = document.getElementById('ulArea');
        oSpan.onclick = function(){
            area.getAreas(function(areas){
                for(var i = 0; i < areas.length; i++){
                    var oLi = document.createElement('li');
                    oLi.innerHTML = areas[i].text;
                    oLi.setAttribute('areaId',areas[i].value);
                    oUl.appendChild(oLi);
                }
            });
        };
        //登录
        var oBtnLogin = document.getElementById('btnLogin');
        oBtnLogin.onclick = function(){
            user.login('zhang3');
        };
        //当前用户
        var oBtnCurUser = document.getElementById('btnCurUser');
        oBtnCurUser.onclick = function(){
            user.getCur();
        };

        //产品列表
        var tmpl = '<img src="#imgUrl#" alt=""><span>#id#-#price#</span><a href="#id#">#name#-#id#</a>';
        var oUlProList = document.getElementById('dvProList').children[0];
        var res = search.getSearchResult('');
        for(var i = 0; i < res.length; i++){
            var oLi = document.createElement('li');

            var re = /\#(\w+)\#/g;
            var html = tmpl.replace(re,function(v){
                return res[i][v.replace(/#/g,'')];

            });

            oLi.innerHTML = html;
            oUlProList.appendChild(oLi);
        }
    });
});